﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProxyWrapper.UserProxy;
using System.Data;

public partial class Comments : System.Web.UI.Page
{
    string userId = "";
    UserDetails _objUser = null;
    ProxyWrapper.UserServiceWrapper _wrap = null;
    string commentId = "";
    string _userName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserObject"] != null)
            _objUser = ((ProxyWrapper.UserProxy.UserDetails)Session["UserObject"]);
        else
            Response.Redirect("Default.aspx", true);
        if (!Page.IsPostBack)
        {
            try
            {
                DataSet ds = null;

                _userName = Request.QueryString["username"] != null ? Request.QueryString["username"].ToString() : "";
                if (Request.QueryString["addedOnId"] != null)
                {
                    userId = Request.QueryString["addedOnId"].ToString();
                    userIdCommented.Value = userId;
                    _wrap = new ProxyWrapper.UserServiceWrapper();
                    ds = _wrap.GetComments(Convert.ToInt32(userId.Trim()));

                    h2.InnerText = "You are adding comment on user : " + _userName;
                    btnSubmit.Text = "Add Comment";
                }
                if (Request.QueryString["commentid"] != null)
                {
                    commentId = Request.QueryString["commentid"].ToString();
                    hdnCommentId.Value = commentId;
                    DataTable dtFiltered = ds.Tables[0].AsEnumerable().Where(m => m.Field<Int32>("CommentId").Equals(Convert.ToInt32(commentId))).CopyToDataTable();
                    txtComment.Text = dtFiltered.Rows[0]["CommentDesc"].ToString();
                    h2.InnerText = "You are updating comment on user : " + _userName + " which was added by : "+ dtFiltered.Rows[0]["AppUserName"].ToString();
                    btnSubmit.Text = "Update Comment";
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            _wrap = new ProxyWrapper.UserServiceWrapper();
            int cid = 0;
            Int32.TryParse(hdnCommentId.Value, out cid);
            string msg = _wrap.AddComments(cid, _objUser.UserId, Convert.ToInt32(userIdCommented.Value), txtComment.Text);
            if (cid <= 0)
                js.InnerHtml = ("<script>Confirmation('" + msg + "');</script>");
            else
                Response.Write("<script>alert('Comment updated successfully!');  window.opener.location.reload(); window.close();</script>");
            // Response.Write(msg);
        }
        catch (Exception)
        {
        }
    }
}